<html>
<head>
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript">
var site = 'http://'+self.location.host+'/'
var xmlhttp;
var an;
var ss = 'p49h16NQcZpfmwyON3r6vGQ'
var ws = 'od6'
var key
var test
var test_url = site+"get/item?ss="+ss+"&ws="+ws;
function load_item(url)
{
	xmlhttp=new XMLHttpRequest();
	xmlhttp.onreadystatechange=state_Change;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
//	document.getElementById('question').value=xmlhttp.responseText;
	var a ; 
	a = 1;
}
function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200){// 200 = OK
    // ...our code here...
        try
	{
	test = eval( "(" + xmlhttp.responseText + ")" );
	document.getElementById('question').value = test.q;
	an = test.a;
	key = test.k;
	document.getElementById('answer').value = "";
	toggle_button(true);
	document.title = test.num[0] + '/' + test.num[1]
	}
	catch (err)
	{
	location.replace(site)
	}
  }
  else{
//  alert("Problem retrieving XML data");
  }
}
}
function toggle_button(state)
{
document.Mioform.goodbutton.disabled=document.Mioform.badbutton.disabled=document.Mioform.sosobutton.disabled = state
}
function refresh()
{
toggle_button(true)
document.getElementById('question').value=document.getElementById('answer').value = "";
load_item(test_url)
}
function send(rate){
	var params = {"q":document.getElementById('question').value,"a":document.getElementById('answer').value,"r":rate,"k":key};
	var body = JSON.stringify(params);
	var req = new XMLHttpRequest();
	var async = false
	req.open('POST', site+'post/item', async);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Content-type", "application/json");
	req.setRequestHeader("Content-length", body.length);
	req.setRequestHeader("Connection", "close");
  if (async) {
    req.onreadystatechange = function() {
      if(req.readyState == 4 && req.status == 200) {
        var response = null;
        try {
         response = eval( "(" + req.responseText + ")" );
        } catch (e) {
         response = req.responseText;
        }
      }
    }
  }
req.send(body);
refresh()
    	
}
function additem(){
	var params = {"q":document.getElementById('question').value,"a":document.getElementById('answer').value,"ss":ss,"ws":ws};
	var body = JSON.stringify(params);
	var req = new XMLHttpRequest();
	var async = false
	req.open('POST', site+'add/item/json', async);
	req.setRequestHeader("Content-type", "application/json");
	req.setRequestHeader("Content-length", body.length);
	req.setRequestHeader("Connection", "close");
  if (async) {
    req.onreadystatechange = function() {
      if(req.readyState == 4 && req.status == 200) {
        var response = null;
        try {
         response = eval( "(" + req.responseText + ")" );
        } catch (e) {
         response = req.responseText;
        }
      }
    }
  }
req.send(body);
refresh()
    	
}

function debug()
{
var a ; var b;
a = test[1]
a = 1;

}


</script>
</head>
<body onload="refresh()">

Welcome {{ user }} ! (<a href="{{ signout }}">sign out</a>)
<form name="Mioform"><br>
<textarea onfocus="if (this.value == 'noreptext') this.value=''" cols="50" rows="3" name="question" id="question"> ciao</textarea>
&nbsp; <input onclick="toggle_button(false);document.Mioform.answer.value=an" name="showanswer" value="show answer" type="button"><br><br><textarea cols="50" rows="3" name="answer" id="answer"></textarea><br><input name="key" type="hidden" value = key  ><br><br>
<input disabled="disabled" value="Good" name="Level" id="goodbutton" type="button" onclick=send("G")>&nbsp; &nbsp; &nbsp;<input disabled="disabled" value="Soso" name="Level" id="sosobutton" type="button" onclick=send("S")>
&nbsp; &nbsp; &nbsp;<input disabled="disabled" id="badbutton" name="Level" value="Bad" type="submit" onclick=send("B")>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; <input name="Add" value="Add" onclick="key='';additem()" type="button">
</form>
<form>
<input type="button" value="Click me!"
onclick="refresh()">

</form>

<br>



</body>
</html>
